package device

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/device"
	deviceReq "github.com/flipped-aurora/gin-vue-admin/server/model/device/request"
)

type DevicesService struct{}

// CreateDevices 创建devices表记录
// Author [yourname](https://github.com/yourname)
func (devicesService *DevicesService) CreateDevices(ctx context.Context, devices *device.Devices) (err error) {
	err = global.GVA_DB.Create(devices).Error
	return err
}

// DeleteDevices 删除devices表记录
// Author [yourname](https://github.com/yourname)
func (devicesService *DevicesService) DeleteDevices(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&device.Devices{}, "id = ?", ID).Error
	return err
}

// DeleteDevicesByIds 批量删除devices表记录
// Author [yourname](https://github.com/yourname)
func (devicesService *DevicesService) DeleteDevicesByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]device.Devices{}, "id in ?", IDs).Error
	return err
}

// UpdateDevices 更新devices表记录
// Author [yourname](https://github.com/yourname)
func (devicesService *DevicesService) UpdateDevices(ctx context.Context, devices device.Devices) (err error) {
	err = global.GVA_DB.Model(&device.Devices{}).Where("id = ?", devices.ID).Updates(&devices).Error
	return err
}

// GetDevices 根据ID获取devices表记录
// Author [yourname](https://github.com/yourname)
func (devicesService *DevicesService) GetDevices(ctx context.Context, ID string) (devices device.Devices, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&devices).Error
	return
}

// GetDevicesInfoList 分页获取devices表记录
// Author [yourname](https://github.com/yourname)
func (devicesService *DevicesService) GetDevicesInfoList(ctx context.Context, info deviceReq.DevicesSearch) (list []device.Devices, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&device.Devices{})
	var devicess []device.Devices
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	if info.DeviceCode != nil && *info.DeviceCode != "" {
		db = db.Where("device_code = ?", *info.DeviceCode)
	}
	if info.DeviceName != nil && *info.DeviceName != "" {
		db = db.Where("device_name LIKE ?", "%"+*info.DeviceName+"%")
	}
	if info.Location != nil && *info.Location != "" {
		db = db.Where("location LIKE ?", "%"+*info.Location+"%")
	}
	if info.OperateStatus != nil && *info.OperateStatus != "" {
		db = db.Where("operate_status = ?", *info.OperateStatus)
	}
	if info.RunStatus != nil && *info.RunStatus != "" {
		db = db.Where("run_status = ?", *info.RunStatus)
	}
	if info.Presence != nil && *info.Presence != "" {
		db = db.Where("presence = ?", *info.Presence)
	}
	if info.ActivateStatus != nil && *info.ActivateStatus != "" {
		db = db.Where("activate_status = ?", *info.ActivateStatus)
	}
	if info.SoftwareStatus != nil && *info.SoftwareStatus != "" {
		db = db.Where("software_status = ?", *info.SoftwareStatus)
	}
	if info.SoftwareVersion != nil && *info.SoftwareVersion != "" {
		db = db.Where("software_version = ?", *info.SoftwareVersion)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&devicess).Error
	return devicess, total, err
}
func (devicesService *DevicesService) GetDevicesPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
